﻿/// <reference path="_namespace.js" />
/// <reference path="GameComponent.js" />
/// <reference path="../drawing/DeviceTypes.js" />

/*****************************************************************************
Ajax library class definition. Kevin Robinson 2008.
Class: ajax.xna.Camera
/*****************************************************************************/
$imports("ajax.math.Matrix");
$imports("ajax.math.Vector3");
$imports("ajax.drawing.DeviceTypes");
ajax.lang.addNamespace("ajax.xna");

//-----------------------------------------------------------------------------
ajax.xna.Camera = function(game)
{
    ajax.xna.Camera.superConstructor.call(this, game);    
}
$extend("ajax.xna.Camera", "ajax.xna.GameComponent");
if ($getDesignMode())
    ajax.xna.Camera.prototype = ajax.xna.GameComponent.prototype;
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Statics first
ajax.xna.Camera.cast = function(obj)
{///<returns type="ajax.xna.Camera"/>
    return obj;
}

//-----------------------------------------------------------------------------
// Public virtual Members
ajax.xna.Camera.prototype.matrix        = ajax.math.Matrix.prototype;
ajax.xna.Camera.prototype.rotationSpeed = 0.001;
ajax.xna.Camera.prototype.moveSpeed     = 0.005;
ajax.xna.Camera.prototype.lastKeystates = [];

ajax.xna.Camera.prototype.initialize = function()
{          
    // Get the game's keyboard to start listening for keys. This is fine if it already was (from another component) as
    // event listeners are only attached once to an object.
    this.getGame().getKeyboard().setListenForKeys(this.getWindow(), this.getGame().topLayer, true);
    
    var Matrix = ajax.math.Matrix;
    // Copy the view matrix into ours
    this.matrix = Matrix.multiply(this.getGame().graphicsDevice.transform.view, Matrix.rotateY(0));
}

ajax.xna.Camera.prototype.update = function(gameTime)
{
    gameTime      = ajax.drawing.Timer.cast(gameTime);
    var Matrix    = ajax.math.Matrix;
    var Vector3   = ajax.math.Vector3;
    
    var game      = this.getGame();
    var device    = this.getGame().graphicsDevice;
    
    var allKeys   = game.getKeyboard().getKeys();    
    var keyStates = this.getGame().getKeyboard().getCurrentKeyboardState(); 
    
    
    var rotSpeed  = gameTime.elapsedTime * this.rotationSpeed;
    var movSpeed  = gameTime.elapsedTime * this.moveSpeed;
    /*
    // remember, when manipulating the view matrix, everything is opposite (view is the inverse of a world matrix).
    if (keyStates[allKeys.KeyNumpad4])
        this.matrix = Matrix.multiply(this.matrix, Matrix.rotateY(rotSpeed));
    if (keyStates[allKeys.KeyNumpad6])
        this.matrix = Matrix.multiply(this.matrix, Matrix.rotateY(-rotSpeed));
    if (keyStates[allKeys.KeyNumpad8])
        this.matrix = Matrix.multiply(this.matrix, Matrix.translation(Vector3.create(0, 0, -movSpeed)));
    if (keyStates[allKeys.KeyNumpad5])
        this.matrix = Matrix.multiply(this.matrix, Matrix.translation(Vector3.create(0, 0, movSpeed)));
    if (keyStates[allKeys.KeyQ])
        this.matrix = Matrix.multiply(this.matrix, Matrix.rotateX(rotSpeed));
    if (keyStates[allKeys.KeyA])
        this.matrix = Matrix.multiply(this.matrix, Matrix.rotateX(-rotSpeed));
    
    // Assign Matrix
    this.getGame().graphicsDevice.transform.view = this.matrix;
    */
    
    if (keyStates[allKeys.KeyW] && !this.lastKeystates[allKeys.KeyW])
    {
        switch (device.renderState.fillMode)
        {
            case ajax.drawing.FillModes.Solid:
                device.renderState.fillMode = ajax.drawing.FillModes.Wireframe;
                break;
            case ajax.drawing.FillModes.Wireframe:
                device.renderState.fillMode = ajax.drawing.FillModes.Point;
                break;
            case ajax.drawing.FillModes.Point:
                device.renderState.fillMode = ajax.drawing.FillModes.Solid;
                break;
        }
    }
        
    if (keyStates[allKeys.KeyR] && !this.lastKeystates[allKeys.KeyR])
        this.getGame().graphicsDevice.renderState.renderingEnabled = !this.getGame().graphicsDevice.renderState.renderingEnabled;
                      
    // Toggle graphics..
    if (keyStates[allKeys.Key1] && !this.lastKeystates[allKeys.Key1])
    {        
        // Dispose of old graphics device
        this.getGame().graphicsDevice.dispose();
        // recreate 3d device
        this.getGame().createGraphics(ajax.drawing.DeviceTypes.Native);        
    }
    if (keyStates[allKeys.Key2] && !this.lastKeystates[allKeys.Key2])
    {        
        this.getGame().graphicsDevice.dispose();
        this.getGame().createGraphics(ajax.drawing.DeviceTypes.Canvas);
    }
    /*
    if (keyStates[allKeys.Key3] && !this.lastKeystates[allKeys.Key3])
    {        
        this.getGame().graphicsDevice.dispose();
        this.getGame().createGraphics(deviceTypes.Java); 
    }
    */
    this.lastKeystates = keyStates;
}

//-----------------------------------------------------------------------------
// Public virtual Methods
ajax.xna.Camera.prototype.toString = function()
{
    return "ajax.xna.Camera";
}
